﻿using Business.Base;
using Business.Base.DB;
using Business.Base.Model;
using Business.Base.Service;

using Business.NPSH;

using PLC.Base;
using Pumptester.ExperimentForms.Base;
using System;

using System.Drawing;

using System.Windows.Forms;

namespace Pumptester.ExperimentForms.Npsh
{
    public partial class MainFormEnhance : Base.BaseMainForm
    {
        public MainFormEnhance(Experiment experiment) : base(experiment)
        {
            InitializeComponent();
            this.dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dataGridView.MergeColumnNames.Add("Column0");
            this.dataGridView.MergeColumnNames.Add("Column1");
            this.dataGridView.MergeColumnNames.Add("Column2");
            this.dataGridView.MergeColumnNames.Add("Column3");
            this.dataGridView.MergeColumnNames.Add("Column4");
            this.dataGridView.MergeColumnNames.Add("Column5");
            this.dataGridView.MergeColumnNames.Add("Column6");


            this.dataGridView.AddSpanHeader(0, 7, "测量数据");

            this.dataGridView.MergeColumnNames.Add("Column7");
            this.dataGridView.MergeColumnNames.Add("Column8");

            this.dataGridView.AddSpanHeader(7, 2, "计算数据");

            this.Experiment = experiment;
            this.dataReadHandle = dataFetch_After;
            //this.baseChart = this.chart;
            this.baseDataGridView = this.dataGridView;
            this.baseDataGridView.ColumnHeaderMouseClick += BaseDataGridView_ColumnHeaderMouseClick;
            this.ChartDrawer = new NpshChart();
            CaculateAndReDrawReport();

        }


        private void dataFetch_After(EnvData data)
        {
            NpshExperimentRecord record = (NpshExperimentRecord)this.ExperimentService.CaculateDataFromRecord(this.Experiment,data);
            this.lblInlet.Text = Math.Round(data.PresureIn, 2).ToString();
            this.lblOutlet.Text = Math.Round(data.PresureOut, 2).ToString();
            this.lblCurrent.Text = Math.Round(data.Current, 2).ToString();
            this.lblVoltage.Text = Math.Round(data.Voltage, 2).ToString();
            this.lblFlow.Text = Math.Round(data.Flow, 2).ToString();
            this.lblPower.Text = Math.Round(data.ShaftPower / 1000.0, 2).ToString();
            this.lblSpeed.Text = Math.Round(data.Speed, 2).ToString();
            this.lblHead.Text = Math.Round(record.WorkingHead, 2).ToString();

        }


        #region 继承的方法和属性

        



        protected override void DrawChart(BaseChartData chartData)
        {
            this.ChartDrawer.DrawChart(chartData, this.userChart);
        }

        protected override void ClearChart()
        {
           
        }
      
        protected override void ShowRecordinDataGridView(ExperimentRecord experimentRecord)
        {
            NpshExperimentRecord record = (NpshExperimentRecord)experimentRecord;

            record.ExperimentCode = this.Experiment.ExperimentCode;
            DataGridViewRow row = (DataGridViewRow)this.dataGridView.RowTemplate.Clone();

            DataGridViewTextBoxCell idCell = new DataGridViewTextBoxCell();
            idCell.Value = 0;
            idCell.Tag = false;

            DataGridViewTextBoxCell flowCell = new DataGridViewTextBoxCell();
            flowCell.Value = record.WorkingFlow.ToString("f6");
            flowCell.Tag = true;

            DataGridViewTextBoxCell speedCell = new DataGridViewTextBoxCell();
            speedCell.Value = record.WorkingSpeed.ToString("f6");
            speedCell.Tag = true;


            DataGridViewTextBoxCell presureInCell = new DataGridViewTextBoxCell();
            presureInCell.Value = record.PresureIn.ToString("f6");
            presureInCell.Tag = true;

            DataGridViewTextBoxCell presureOutCell = new DataGridViewTextBoxCell();
            presureOutCell.Value = record.PresureOut.ToString("f6");
            presureOutCell.Tag = true;




            DataGridViewTextBoxCell powerCell = new DataGridViewTextBoxCell();
            powerCell.Value = (record.WorkingPower / 1000.0).ToString("f6");
            powerCell.Tag = true;


            DataGridViewTextBoxCell temperatureCell = new DataGridViewTextBoxCell();
            temperatureCell.Value = record.Temperature.ToString("f6");
            temperatureCell.Tag = true;


            DataGridViewTextBoxCell npshCell = new DataGridViewTextBoxCell();
            npshCell.Value = record.Npsh.ToString("f6");
            npshCell.Tag = true;


            DataGridViewTextBoxCell headCell = new DataGridViewTextBoxCell();
            headCell.Value = record.WorkingHead.ToString("f6");
            headCell.Tag = true;

            row.Cells.Add(idCell);
            row.Cells.Add(flowCell);
            row.Cells.Add(presureInCell);
            row.Cells.Add(presureOutCell);
            row.Cells.Add(speedCell);
            row.Cells.Add(temperatureCell);
            row.Cells.Add(powerCell);
            row.Cells.Add(npshCell);
            row.Cells.Add(headCell);

            row.Tag = record;


            //this.experiment.RecordList.Add(record);
            this.dataGridView.Rows.Add(row);
            int id = this.ExperimentService.SaveExperimentRecord(record);
            record.Id = id;
            refreshIdCell();
            //计算和重画报表
            CaculateAndReDrawReport();
        }

        protected override DynamicParams GetParams()
        {
            NpshExperiment exp = (NpshExperiment)this.Experiment;
            DynamicParams dynamicParams = new DynamicParams(exp.FlowmeterAddress, exp.TransformationRatio, (ushort)exp.PhassAddress);
            return dynamicParams;
        }

        protected override DialogResult ShowEditDialog()
        {
            NPSHEditForm form = new NPSHEditForm(this.Experiment);
            return form.ShowDialog();
        }

        #endregion

        private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex<1 && e.ColumnIndex >6 )
            {
                return;
            }
            DataGridView dataGridView = (DataGridView)sender;

            bool canSet = (bool)dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag;
            if (!canSet)
            {
                return;
            }

            String labelText = dataGridView.Columns[e.ColumnIndex].HeaderText;

            String oldValue = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            DataGridViewRow row = dataGridView.Rows[e.RowIndex];
            NpshExperimentRecord record = (NpshExperimentRecord)row.Tag;
            ValueEditForm editForm = new ValueEditForm(labelText, oldValue);

            System.Drawing.Point point = Control.MousePosition;
            editForm.Location = point;
            var result = editForm.ShowDialog();

            if (DialogResult.OK == result)
            {
                try
                {
                    dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = editForm.ReturnValue;
                }
                catch
                {
                    return;
                }
                try
                { 
                switch (e.ColumnIndex)
                {
                    case 1: record.WorkingFlow  = Convert.ToDouble(editForm.ReturnValue);            break;
                    case 2: record.PresureIn    = Convert.ToDouble(editForm.ReturnValue);            break;
                    case 3: record.PresureOut   = Convert.ToDouble(editForm.ReturnValue);            break;
                    case 4: record.WorkingSpeed = Convert.ToDouble(editForm.ReturnValue);            break;
                    case 5: record.Temperature  = Convert.ToDouble(editForm.ReturnValue);            break;
                    case 6: record.WorkingPower = Convert.ToDouble(editForm.ReturnValue) * 1000.0;   break;
                }

                }
                catch (FormatException ex)
                {
                    MessageBox.Show("录入的数据格式错误！");
                    
                    return;
                }



                //重新计算
                //EnvData data = new EnvData(record.PresureIn, record.PresureOut, record.WorkingFlow, record.Temperature, record.WorkingSpeed, record.Voltage, record.ElectricCurrent, record.WorkingPower, 1.0f);
                EnvData data = new EnvData();
                data.PresureIn = record.PresureIn;
                data.PresureOut = record.PresureOut;
                data.Flow = record.WorkingFlow;
                data.Temperature = record.Temperature;
                data.Speed = record.WorkingSpeed;
                data.Voltage = record.Voltage;
                data.Current = record.ElectricCurrent;
                data.ShaftPower = record.WorkingPower;

                NpshExperimentRecord newRecord = (NpshExperimentRecord)this.ExperimentService.CaculateDataFromRecord(this.Experiment, data);

                //重新刷新数据
                record.Npsh = newRecord.Npsh;
                record.WorkingHead  = newRecord.WorkingHead;


                this.ExperimentService.UpdateExperimentRecord(record);

                dataGridView.Rows[e.RowIndex].Cells[7].Value = record.Npsh.ToString("F6");
                dataGridView.Rows[e.RowIndex].Cells[8].Value = record.WorkingHead.ToString("F6"); ;
              
                CaculateAndReDrawReport();

            }
        }
    }
}
